package com.zhang.authoritycenter.controller.token;

import com.zhang.authoritycenter.common.aop.OperatorLog;
import com.zhang.authoritycenter.common.model.dao.auth.UserBaseDO;
import com.zhang.authoritycenter.common.model.request.LoginDTO;
import com.zhang.authoritycenter.common.model.request.RegisterReqDTO;
import com.zhang.authoritycenter.manager.IOauthManager;
import com.zhang.authoritycenter.service.LoginService;
import com.zhang.infrastructure.model.KingHoodResponseVO;
import com.zhang.infrastructure.model.token.KingHoodPrincipal;
import com.zhang.infrastructure.model.token.UserDTO;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RestController;

/**
 * description
 *
 * @author zhangFanJun
 * @date 2023-12-06 10:42
 **/
@RequiredArgsConstructor
@RestController
public class LoginController {

    private final IOauthManager IOauthManager;
    private final LoginService loginService;

    /**
     * 注册
     *
     * @param in 注册入参
     * @return 用户信息
     * @author zfj
     * @date 2023/12/6
     */
    @PostMapping("/register")
    @OperatorLog
    public KingHoodResponseVO<UserBaseDO> register(@RequestBody RegisterReqDTO in) {
        return KingHoodResponseVO.success(IOauthManager.register(in));
    }

    /**
     * 登录
     *
     * @param in 登录入参
     * @return 用户信息
     * @author zfj
     * @date 2023/12/6
     */
    @PostMapping("/login")
    @OperatorLog
    public KingHoodResponseVO<KingHoodPrincipal<UserDTO>> login(@RequestBody LoginDTO in) {
        return KingHoodResponseVO.success(IOauthManager.login(in));
    }

    /**
     * 注销
     * jwt的有状态性可以从两个角度考虑
     * 正向：每次记录token到redis，当退出登录的时候，就删除token，这个时候token的校验需要判断redis是否存在token
     * 反向：退出登录的时候记录，失效时间等于token的有效期，有效期不能太长，避免出现大量的无效数据
     *
     * 从实际出发，考虑反向黑名单的方式
     *
     * @return 成功与否
     * @author zfj
     * @date 2023/12/6
     */
    @PostMapping("/logout")
    @OperatorLog
    public KingHoodResponseVO<Boolean> logout(@RequestHeader("Authorization")String bear) {
        return KingHoodResponseVO.success(IOauthManager.logout(bear));
    }

    /**
     * 由于logout可能与内部的接口冲突，导致无法调通
     * */
    @PostMapping("/out")
    @OperatorLog
    public KingHoodResponseVO<Boolean> out(@RequestHeader("Authorization")String bear) {
        return KingHoodResponseVO.success(IOauthManager.logout(bear));
    }


}
